package com.hkyp.ss.course.provider.impl;

import com.hkyp.ss.course.apis.AdminUserService;
import com.hkyp.ss.course.apis.UserService;
import com.hkyp.ss.course.apis.cons.CommonConstants;
import com.hkyp.ss.course.apis.cons.JwtConstants;
import com.hkyp.ss.course.apis.dto.request.*;
import com.hkyp.ss.course.apis.dto.response.UserAvatarRequestDTO;
import com.hkyp.ss.course.apis.dto.response.UserBaseInfoDTO;
import com.hkyp.ss.course.apis.dto.response.UserHomePageVO;
import com.hkyp.ss.course.apis.enumu.UserStatusEnum;
import com.hkyp.ss.course.apis.exception.BusinessException;
import com.hkyp.ss.course.apis.exception.DaoException;
import com.hkyp.ss.course.apis.exception.UnacceptableException;
import com.hkyp.ss.course.apis.utils.BeanMapper;
import com.hkyp.ss.course.apis.utils.JWTTokenUtil;
import com.hkyp.ss.course.apis.utils.third_party_api.EmailUtil;
import com.hkyp.ss.course.provider.model.AdminUser;
import com.hkyp.ss.course.provider.model.User;
import com.hkyp.ss.course.provider.repository.AdminUserRepository;
import com.hkyp.ss.course.provider.repository.UserRepository;
import com.hkyp.ss.course.provider.repository.mediate.UURelationRepository;
import com.hkyp.ss.course.provider.util.AESUtil;
import com.hkyp.ss.course.provider.util.Md5Util;
import com.hkyp.ss.course.provider.util.PwdUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

//@Component
@Slf4j
@Service(version = "1.0")
@Transactional(rollbackFor = Exception.class)
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private AdminUserRepository adminUserRepository;



    @Override
    public String login(AdminLoginRequestDTO loginDTO) throws UnacceptableException, BusinessException {
        AdminUser adminUser = adminUserRepository.findByNickName(loginDTO.getNickName());
        if(adminUser == null){
            throw new UnacceptableException("用户不存在");
        }
        //密码： 47270d0574c5ae0663150a793d9df64d
        if(!Objects.equals(PwdUtil.getPasswordEncrypted(loginDTO.getPassword(),loginDTO.getNickName()),adminUser.getPassword())){
            throw new UnacceptableException("密码错误");
        }
        Map<String,String> map = new HashMap();
        map.put(JwtConstants.UserId,String.valueOf(adminUser.getId()));
        map.put(JwtConstants.tokenType,JwtConstants.backPlatform);
        String token = JWTTokenUtil.signWithMap(map, JWTTokenUtil.DEF_EXP);

        return token;
    }


    // test

    public static void main(String[] args) {
        System.out.println(Md5Util.string2MD5(Md5Util.string2MD5("acer4745")+"1322364968@qq.com"));
        AtomicInteger atomicInteger = new AtomicInteger(5);
        atomicInteger.addAndGet(1);
    }
}
